﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Common;
using XF.Common.WeChat;
using XF.Meeting.DAL;

namespace QuartzJob.Jobs
{
    public class SendMeetEndToCreator : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                var dal = new MeetingDal();
                var meetids = dal.GetUserListForMeetEnd(DateTime.Now, out string StrError);
                if (StrError == "")
                {
                    var list1 = TableToList.ToList<meetIdS>(meetids.Result);
                    if (list1.Count > 0)
                    {
                        foreach (var item in list1)
                        {
                            SendWxMssage(item.ID, true);
                        }
                        return Task.CompletedTask;
                    }
                    else
                    {
                        return Task.CompletedTask;
                    }
                }
                return Task.CompletedTask;
            }
            catch (Exception e)
            {

                return Task.CompletedTask;
            }

        }
        public static void SendWxMssage(int ID, bool isfifteen = false)
        {
            var dal = new MeetingDal();
            var res = dal.GetCInfoByID(ID).Result;

            var list = TableToList.ToList<XF.Meeting.Entity.View.MeetingInfoSingle>(res);
            var meetingInfoSingle = list[0];
            meetingInfoSingle.date = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("yyyy-MM-dd");
            meetingInfoSingle.Begin = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("HH:mm");
            meetingInfoSingle.End = Convert.ToDateTime(meetingInfoSingle.EndTime).ToString("HH:mm");
            meetingInfoSingle.CRooms = meetingInfoSingle.CRoom + "," + meetingInfoSingle.CRooms;
            meetingInfoSingle.CRooms = meetingInfoSingle.CRooms.Substring(0, meetingInfoSingle.CRooms.Length - 1);
            var msgList = dal.getMeetInfoAndUserPhone(ID, out string StrError);
            var phone = TableToList.ToList<meetIdS>(msgList.Tables[1]).FirstOrDefault(); 

            var userr = phone.Account;
          
            var msg = new Message(new Text($"{meetingInfoSingle.CoordinatorName}:您预定的《{meetingInfoSingle.ConferenceTheme}》会议，时间 {meetingInfoSingle.date}  {meetingInfoSingle.Begin}~{meetingInfoSingle.End}，地点：{meetingInfoSingle.CRooms}，该会议将在15分钟后结束。"), userr);

            switch (meetingInfoSingle.wxType)
            {
                case 1:
                    var wx1 = new WeChatHelper();
                    wx1.SendTextMessage(msg);
                    break;
                case 2:
                    var wx2 = new WyWeChatHelper();
                    wx2.SendTextMessage(msg);
               
                    break;
                default:
                    var wx3 = new WeChatHelper();
                    wx3.SendTextMessage(msg);
                    break;
            }
            if (isfifteen)
            {
                dal.UpdateWXEndState(ID, out string strError);
            }


        }
        public class meetIdS
        {
            public int ID { get; set; }
            public string Phone { get; set; }
            public string Account { get; set; }
        }
    }
}
